package com.tj.tj_java.service.impl;

import com.tj.tj_java.dto.DanmuDto;
import com.tj.tj_java.mapper.VideoMapper;
import com.tj.tj_java.pojo.Danmu;
import com.tj.tj_java.pojo.Video;
import com.tj.tj_java.result.Result;
import com.tj.tj_java.service.VideoService;
import com.tj.tj_java.vo.DanmuVo;
import com.tj.tj_java.vo.VideoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    @Override
    //获取视频列表
    public Result getAllVideoList(int userId) {
        //System.out.println(userId);
        //获取视频列表
        List<Video> videoList = videoMapper.selectAllVideo();
        ////创建保存该视频下所有视频信息的实例
        List<VideoVo> videoVoList = new ArrayList<>();
        for (Video item : videoList){
            //根据videoId获取当前视频的全部弹幕
            List<Danmu> danmuList = videoMapper.selectDanmuByVideoId(item.getVideoId());
            //创建保存该视频下所有弹幕的实例
            List<DanmuVo> danmuVoList = new ArrayList<>();
            //遍历该视频的全部弹幕，并构建
            for (Danmu danmuItem : danmuList){
                //将边框属性的1和0转发为为 false 或 ture
                boolean PD = danmuItem.getDanmuBorder() == 1;
                DanmuVo danmuVo = DanmuVo.builder()
                        .text(danmuItem.getDanmuText())
                        .time(danmuItem.getDanmuTime())
                        .color(danmuItem.getDanmuColor())
                        .mode(danmuItem.getDanmuMode())
                        .border(PD)
                        .build();
                danmuVoList.add(danmuVo);
                //System.out.println("该视频全部弹幕"+danmuVoList);
            }
            //构建一个视频完整对象
            VideoVo videoVo = VideoVo.builder()
                    .videoId(item.getVideoId())
                    .videoTitle(item.getVideoTitle())
                    .videoIntro(item.getVideoIntro())
                    .videoPoster(item.getVideoPoster())
                    .videoUrl(item.getVideoUrl())
                    .videoThumbnail(item.getVideoThumbnail())
                    .thumbnailNumber(item.getThumbnailNumber())
                    .videoDanmukuList(danmuVoList)
                    .build();
            videoVoList.add(videoVo);
            //System.out.println("完整对象"+videoVo);
        }
        return Result.success("成功获取全部视频列表数据",videoVoList);
    }


    //根据用户id 视频id获取一首歌曲
    @Override
    public Result getVideo(int userId, int videoId) {
        //System.out.println(userId); userid没有用上
        //通过id获取一个
       Video aVideo = videoMapper.selectVideoById(videoId);
       //获取该视频的全部弹幕
        List<Danmu> danmuList = videoMapper.selectDanmuByVideoId(aVideo.getVideoId());
        //创建弹幕返回类
        List<DanmuVo> danmuVoList = new ArrayList<>();
        //遍历该视频的全部弹幕，并构建
        for (Danmu danmuItem : danmuList){
            //将边框属性的1和0转发为为 false 或 ture
            boolean PD = danmuItem.getDanmuBorder() == 1;
            DanmuVo danmuVo = DanmuVo.builder()
                    .text(danmuItem.getDanmuText())
                    .time(danmuItem.getDanmuTime())
                    .color(danmuItem.getDanmuColor())
                    .mode(danmuItem.getDanmuMode())
                    .border(PD)
                    .build();
            danmuVoList.add(danmuVo);
            //System.out.println("该视频全部弹幕"+danmuVoList);
        }
        VideoVo videoVo =  VideoVo.builder()
                .videoId(aVideo.getVideoId())
                .videoTitle(aVideo.getVideoTitle())
                .videoIntro(aVideo.getVideoIntro())
                .videoPoster(aVideo.getVideoPoster())
                .videoUrl(aVideo.getVideoUrl())
                .videoThumbnail(aVideo.getVideoThumbnail())
                .thumbnailNumber(aVideo.getThumbnailNumber())
                .videoDanmukuList(danmuVoList)
                .build();

        return Result.success("成功获取该视频",videoVo);
    }


    //添加弹幕实现
    @Override
    public Result addDanmu(DanmuDto danmuDto) {
        //System.out.println("打印弹幕"+danmuDto);
        videoMapper.addDanmu(danmuDto.getUserId(), danmuDto.getVideoId(), danmuDto.getText(),danmuDto.getTime(), danmuDto.getColor(),danmuDto.isBorder(), danmuDto.getMode() );
        return Result.success("成功");
    }
}
